﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml; 

namespace JoomlaXMLEditor
{
    /// <summary>
    /// edytor informacji o szablonie
    /// </summary>
    public partial class TemplateInformations : Form
    {
        // uchwyt do okna głónego
        private MainWindow parent;
        // konstruktor
        public TemplateInformations(MainWindow parent)
        {
            InitializeComponent();
            this.parent = parent;
            if(this.parent.actualFile.GetElementsByTagName("name")[0].HasChildNodes)
                this.textBox1.Text = this.parent.actualFile.GetElementsByTagName("name")[0].FirstChild.Value;
            if (this.parent.actualFile.GetElementsByTagName("creationDate")[0].HasChildNodes)
                this.textBox2.Text = this.parent.actualFile.GetElementsByTagName("creationDate")[0].FirstChild.Value;
            if (this.parent.actualFile.GetElementsByTagName("author")[0].HasChildNodes)
                this.textBox3.Text = this.parent.actualFile.GetElementsByTagName("author")[0].FirstChild.Value;
            if (this.parent.actualFile.GetElementsByTagName("authorEmail")[0].HasChildNodes)    
                this.textBox4.Text = this.parent.actualFile.GetElementsByTagName("authorEmail")[0].FirstChild.Value;
            if (this.parent.actualFile.GetElementsByTagName("authorUrl")[0].HasChildNodes)    
                this.textBox5.Text = this.parent.actualFile.GetElementsByTagName("authorUrl")[0].FirstChild.Value;
            if (this.parent.actualFile.GetElementsByTagName("copyright")[0].HasChildNodes)    
                this.textBox6.Text = this.parent.actualFile.GetElementsByTagName("copyright")[0].FirstChild.Value;
            if (this.parent.actualFile.GetElementsByTagName("license")[0].HasChildNodes)    
                this.textBox7.Text = this.parent.actualFile.GetElementsByTagName("license")[0].FirstChild.Value;
            if (this.parent.actualFile.GetElementsByTagName("version")[0].HasChildNodes)    
                this.textBox8.Text = this.parent.actualFile.GetElementsByTagName("version")[0].FirstChild.Value;
            if (this.parent.actualFile.GetElementsByTagName("description")[0].HasChildNodes)    
                this.richTextBox1.Text = this.parent.actualFile.GetElementsByTagName("description")[0].FirstChild.Value;
        }
        // zamykanie
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        // zapisywanie
        private void button1_Click(object sender, EventArgs e)
        {
            if( Validator.basicCheck(this.textBox1.Text) &&
                Validator.basicCheck(this.textBox2.Text) &&
                Validator.basicCheck(this.textBox3.Text) &&
                Validator.basicCheck(this.textBox4.Text) &&
                Validator.basicCheck(this.textBox5.Text) &&
                Validator.basicCheck(this.textBox6.Text) &&
                Validator.basicCheck(this.textBox7.Text) &&
                Validator.basicCheck(this.textBox8.Text) &&
                Validator.basicCheck(this.richTextBox1.Text))
            {
                String[] tags = new String[] {
                                                "name", 
                                                "creationDate", 
                                                "author", 
                                                "authorEmail", 
                                                "authorUrl", 
                                                "copyright", 
                                                "license", 
                                                "version", 
                                                "description" 
                                            };

                foreach(String name in tags) 
                {
                    if (!this.parent.actualFile.GetElementsByTagName("description")[0].HasChildNodes)
                    {
                        XmlNode node = this.parent.actualFile.CreateNode(XmlNodeType.Text,"","");
                        this.parent.actualFile.GetElementsByTagName(name)[0].AppendChild(node);
                    }
                }

                this.parent.actualFile.GetElementsByTagName("name")[0].FirstChild.Value = this.textBox1.Text;
                this.parent.actualFile.GetElementsByTagName("creationDate")[0].FirstChild.Value = this.textBox2.Text;
                this.parent.actualFile.GetElementsByTagName("author")[0].FirstChild.Value = this.textBox3.Text;
                this.parent.actualFile.GetElementsByTagName("authorEmail")[0].FirstChild.Value = this.textBox4.Text;
                this.parent.actualFile.GetElementsByTagName("authorUrl")[0].FirstChild.Value = this.textBox5.Text;
                this.parent.actualFile.GetElementsByTagName("copyright")[0].FirstChild.Value = this.textBox6.Text;
                this.parent.actualFile.GetElementsByTagName("license")[0].FirstChild.Value = this.textBox7.Text;
                this.parent.actualFile.GetElementsByTagName("version")[0].FirstChild.Value = this.textBox8.Text;
                this.parent.actualFile.GetElementsByTagName("description")[0].FirstChild.Value = this.richTextBox1.Text;
                
                new TemplateXML(this.parent).refreshTree(this.parent.XMLTree, this.parent.actualFile);
                this.Close();
            }
            else
            {
                MessageBox.Show("Proszę sprawdzić poprawność wypełnienia pół formularza.");
            }
        }
    }
}
